from django.http import HttpResponse
from django.shortcuts import render
from meiduo_mall.libs.captcha.captcha import captcha

# Create your views here.
from rest_framework.views import APIView
from django_redis import get_redis_connection
from . import constants

class ImageCodeView(APIView):
    """图片验证码"""
    def get(self, request, image_code_id):

        # 生成图片验证码
        text, image = captcha.generate_captcha()

        # 保存验证码的真实值
        redis_conn = get_redis_connection('verify_code')
        # 将图片的id,有效时间,图片的真实值保存到redis中
        redis_conn. redis_conn.setex("img_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)

        # 将图片返回给前端
        return HttpResponse(image, content_type='image/jpg')


    def a(self):
        pass